package controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.ManagerInfo;
import model.UserInfo;
import service.UserService;

@WebServlet(name="/LoginRegisterServlet",urlPatterns="/user")
public class LoginRegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public LoginRegisterServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		System.out.println("进入doPost方法");
		String methodName = request.getParameter("method");
		System.out.println(methodName);
		if(methodName.equals("login")) {
			String userName  = request.getParameter("username");
			String passWord = request.getParameter("password");
			UserService userService = new UserService();
			try {
				boolean result = userService.checkLogin(userName, passWord);
				if(result) {
					//Cookie存下用户名
					Cookie nameCookie = new Cookie("name",userName);
//					Cookie passwordCookie = new Cookie("password", passWord);
					//设置cookie存活时间！存活时间过了则不会自动填充账号密码
					nameCookie.setMaxAge(60*10*10*10);
//					passwordCookie.setMaxAge(60*10*10*10);
					//往session添加userinfo信息
//					UserInfo userInfo = new UserInfo();
//					request.getSession().setAttribute("userInfo", userInfo);
					response.addCookie(nameCookie);
//					response.addCookie(passwordCookie);
					response.sendRedirect("/phone-introduce/Main.jsp");
				}else {
					response.sendRedirect("/phone-introduce/LoginRegister.jsp");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(methodName.equals("ManagerLogin")) {
			String managerName  = request.getParameter("managername");
			String passWord = request.getParameter("password");
			UserService userService = new UserService();
			try {
				boolean result = userService.checkManagerLogin(managerName, passWord);
				if(result) {
					//往session添加managerinfo信息
					ManagerInfo managerInfo = new ManagerInfo();
					request.getSession().setAttribute("managerInfo", managerInfo);
					response.sendRedirect("/phone-introduce/Manage_Page.jsp");
				}else {
					response.sendRedirect("/phone-introduce/BackStageLogin.jsp");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(methodName.equals("register")) {
			String userName  = request.getParameter("username");
			String passWord = request.getParameter("password");
			UserService userService = new UserService();
			try {
				boolean result = userService.checkRegister(userName, passWord);
				if(result) {
					response.sendRedirect("/phone-introduce/SuccessSkip.jsp");
				}else {
					response.sendRedirect("/phone-introduce/FailedSkip.jsp");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
